<template>
  <div class="tabbar">
      <div class="wrap tabbarwrap">
        <div class="left">欢迎来到叩丁狼积分商城</div>
        <ul class="right">
          <li>
            <img :src="userInfo.headImg" width="23px" alt="">
            用户名：{{userInfo.nickName}}
          </li>
          <li>我的积分：{{userInfo.coin}}</li>
          <li>获取积分</li>
          <li>叩丁狼官网</li>

          <li class="cart-btn" v-if="isLogined">
            <img src="../assets/img/cart.png" alt="">
            <span>购物车</span>
            <strong>{{cartTotal}}</strong>
          </li>
          <li class="btnlogin" @click="changeIsShowLoginModal(true)" v-else>登录</li>
        </ul>
      </div>
  </div>
</template>

<script>
import {mapState,mapMutations,mapActions} from "vuex"
import { WeiXinLoginAPI } from "../request/api"
export default {
  async created(){
    console.log("topbar-created..",this.$route.query.code);
    let mycode = this.$route.query.code
    if(mycode){
      let res = await WeiXinLoginAPI({
        code:mycode
      })
      if(res.code == 0){
        //1.提示
        alert("扫码登录成功")
        //2.存token
        localStorage.setItem("x-auth-token",res["x-auth-token"])
        //3.清除code
        this.$route.query.code && this.$router.push(this.$route.path);
        //4.更新登录状态
        this.changeIsLogined(true)
        //5.更新用户信息
        this.asyncChangeUserInfo();
      }else if(res.code == 400){
        //1. 提示重新扫码
        alert("请重新扫码")
        //2. 帮用户打开登录窗口
        this.changeIsShowLoginModal(true)
      }else if(res.code == 407){
        //1. 提示用户需要手机号再登录一次
        alert("请使用手机号在登录一下")
         // 2.帮用户打开登录窗口
         this.changeIsShowLoginModal(true)
         // 3.保存uuid
         localStorage.setItem("uuid",res.uuid)
      }
      //  console.log("扫码结果：",res);
    }else{
      console.log("没有保存就进来了");
      let mytoken = localStorage.getItem("x-auth-token")
      this.changeIsLogined(Boolean(mytoken))
      if(mytoken){
        this.asyncChangeUserInfo();
      } else{
        console.log("初始化");
        this.initUserInfo();
      }
    }
  },
  watch:{
    // "$route.path":{
    //   handler(newVal,oldVal){
    //     console.log("地址变化了",newVal,oldVal);
    //     let mytoken = localStorage.getItem("x-auth-token")
    //     this.changeIsLogined(Boolean(mytoken))
    //   }
    // }
  },
  computed:{
    ...mapState({
      isLogined: state => state.loginStatus.isLogined,
      cartTotal: state => state.userInfo.cartTotal,
      userInfo: state => state.userInfo.userInfo
    })
  },
  methods:{
    ...mapMutations({
      changeIsShowLoginModal:"showModal/changeIsShowLoginModal",
      changeIsShowToast:"showToast/changeIsShowToast",
      changeIsLogined:"loginStatus/changeIsLogined",
      changeUserInfo:"userInfo/changeUserInfo",
      initUserInfo:"userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeUserInfo:"userInfo/asyncChangeUserInfo"
    })
  }
}
</script>

<style lang="less" scoped>
.tabbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  color: #fffefe;
  .tabbarwrap {
    display: flex;
    justify-content: space-between;
  }
  .right {
    li {
      float: left;
      margin-left: 20px;
      cursor: pointer;
      text-align: center;
      img {
        vertical-align: middle;
        border-radius: 50%;
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center; 
        img{
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span{
          margin: 0 8px;
        }
        strong{
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
      &.btnlogin {
        width: 124px;
        height: 40px;
        background: #0a328e;
      }
    }
  }
}
</style>